package com.openbravo.pos.payment;

import com.openbravo.data.loader.LocalRes;
import com.openbravo.pos.forms.AppLocal;
import com.openbravo.pos.forms.AppProperties;
import com.openbravo.pos.util.AltEncrypter;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class PaymentGatewayLinkPoint
  implements PaymentGateway
{
  private static final String SALE = "SALE";
  private static final String REFUND = "CREDIT";
  private static String HOST;
  private static final int PORT = 1129;
  private String sClientCertPath;
  private String sPasswordCert;
  private String sConfigfile;
  private boolean m_bTestMode;
  private static String APPROVED = "APPROVED";

  public PaymentGatewayLinkPoint(AppProperties paramAppProperties)
  {
    this.m_bTestMode = Boolean.valueOf(paramAppProperties.getProperty("payment.testmode")).booleanValue();
    this.sConfigfile = paramAppProperties.getProperty("payment.commerceid");
    this.sClientCertPath = paramAppProperties.getProperty("payment.certificatePath");
    AltEncrypter localAltEncrypter = new AltEncrypter("cypherkey");
    this.sPasswordCert = localAltEncrypter.decrypt(paramAppProperties.getProperty("payment.certificatePassword").substring(6));
    HOST = this.m_bTestMode ? "staging.linkpt.net" : "secure.linkpt.net";
  }

  public PaymentGatewayLinkPoint()
  {
  }

  public void execute(PaymentInfoMagcard paramPaymentInfoMagcard)
  {
    String str1 = "";
    System.setProperty("javax.net.ssl.keyStore", this.sClientCertPath);
    System.setProperty("javax.net.ssl.keyStorePassword", this.sPasswordCert);
    System.setProperty("javax.net.ssl.keyStoreType", "PKCS12");
    try
    {
      URL localURL = new URL(new StringBuilder().append("https://").append(HOST).append(":").append(1129).toString());
      HttpsURLConnection localHttpsURLConnection = (HttpsURLConnection)localURL.openConnection();
      localHttpsURLConnection.setHostnameVerifier(new NullHostNameVerifier( ));
      localHttpsURLConnection.setDoOutput(true);
      localHttpsURLConnection.setUseCaches(false);
      localHttpsURLConnection.setAllowUserInteraction(false);
      localHttpsURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
      StringBuilder localObject = createOrder(paramPaymentInfoMagcard);
      String str2 = ((StringBuilder)localObject).toString();
      DataOutputStream localDataOutputStream = new DataOutputStream(localHttpsURLConnection.getOutputStream());
      localDataOutputStream.write(((StringBuilder)localObject).toString().getBytes());
      localDataOutputStream.flush();
      BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(localHttpsURLConnection.getInputStream()));
      str1 = localBufferedReader.readLine();
    }
    catch (IOException localIOException)
    {
      paramPaymentInfoMagcard.paymentError(LocalRes.getIntString("exception.iofile"), localIOException.getMessage());
    }
    LinkPointParser localLinkPointParser = new LinkPointParser(str1);
    Object localObject = localLinkPointParser.splitXML();
    if (localLinkPointParser.getResult().equals(LocalRes.getIntString("button.ok")))
    {
      if (APPROVED.equals(((Map)localObject).get("r_approved")))
        paramPaymentInfoMagcard.paymentOK((String)((Map)localObject).get("r_code"), (String)((Map)localObject).get("r_ordernum"), str1);
      else
        paramPaymentInfoMagcard.paymentError(AppLocal.getIntString("message.paymenterror"), (String)((Map)localObject).get("r_error"));
    }
    else
      paramPaymentInfoMagcard.paymentError(localLinkPointParser.getResult(), "");
  }

  private StringBuilder createOrder(PaymentInfoMagcard paramPaymentInfoMagcard)
  {
    StringBuilder localStringBuilder1 = new StringBuilder();
    StringBuilder localStringBuilder2 = new StringBuilder();
    String str1 = paramPaymentInfoMagcard.getTotal() > 0.0D ? "SALE" : "CREDIT";
    DecimalFormat localDecimalFormat = new DecimalFormat("0000.00");
    String str2 = localDecimalFormat.format(Math.abs(paramPaymentInfoMagcard.getTotal()));
    String str3 = paramPaymentInfoMagcard.getExpirationDate();
    String str4 = str1.equals("CREDIT") ? new StringBuilder().append("<oid>").append(paramPaymentInfoMagcard.getTransactionID()).append("</oid>").toString() : "";
    try
    {
      if (paramPaymentInfoMagcard.getTrack1(true) == null)
      {
        localStringBuilder1.append("<creditcard>");
        localStringBuilder1.append(new StringBuilder().append("<cardnumber>").append(paramPaymentInfoMagcard.getCardNumber()).append("</cardnumber> ").toString());
        localStringBuilder1.append(new StringBuilder().append("<cardexpmonth>").append(str3.charAt(0)).append("").append(str3.charAt(1)).append("</cardexpmonth>").toString());
        localStringBuilder1.append(new StringBuilder().append("<cardexpyear>").append(str3.charAt(2)).append("").append(str3.charAt(3)).append("</cardexpyear>").toString());
        localStringBuilder1.append("</creditcard>");
      }
      else
      {
        localStringBuilder1.append("<creditcard>");
        localStringBuilder1.append("<track>");
        localStringBuilder1.append(paramPaymentInfoMagcard.getTrack1(true));
        localStringBuilder1.append("</track>");
        localStringBuilder1.append("</creditcard>");
      }
      localStringBuilder2.append("<order>");
      localStringBuilder2.append(new StringBuilder().append("<merchantinfo><configfile>").append(this.sConfigfile).append("</configfile></merchantinfo>").toString());
      localStringBuilder2.append(new StringBuilder().append("<orderoptions><ordertype>").append(str1).append("</ordertype><result>LIVE</result></orderoptions>").toString());
      localStringBuilder2.append(new StringBuilder().append("<payment><chargetotal>").append(URLEncoder.encode(str2.replace(',', '.'), "UTF-8")).append("</chargetotal></payment>").toString());
      localStringBuilder2.append(localStringBuilder1);
      localStringBuilder2.append("<transactiondetails>");
      localStringBuilder2.append(str4);
      localStringBuilder2.append("<transactionorigin>RETAIL</transactionorigin><terminaltype>POS</terminaltype></transactiondetails>");
      localStringBuilder2.append("</order>");
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
      paramPaymentInfoMagcard.paymentError(AppLocal.getIntString("message.paymentexceptionservice"), localUnsupportedEncodingException.getMessage());
    }
    return localStringBuilder2;
  }

  private class LinkPointParser extends DefaultHandler
  {
    private SAXParser m_sp = null;
    private Map props = new HashMap();
    private String text;
    private InputStream is;
    private String result;

    public LinkPointParser(String arg2)
    {
      String str1 = "";
      String str2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><returned>" + str1 + "</returned>";
      this.is = new ByteArrayInputStream(str2.getBytes());
    }

    public Map splitXML()
    {
      try
      {
        if (this.m_sp == null)
        {
          SAXParserFactory localSAXParserFactory = SAXParserFactory.newInstance();
          this.m_sp = localSAXParserFactory.newSAXParser();
        }
        this.m_sp.parse(this.is, this);
      }
      catch (ParserConfigurationException localParserConfigurationException)
      {
        this.result = LocalRes.getIntString("exception.parserconfig");
      }
      catch (SAXException localSAXException)
      {
        this.result = LocalRes.getIntString("exception.xmlfile");
      }
      catch (IOException localIOException)
      {
        this.result = LocalRes.getIntString("exception.iofile");
      }
      this.result = LocalRes.getIntString("button.ok");
      return this.props;
    }

    public void endElement(String paramString1, String paramString2, String paramString3)
      throws SAXException
    {
      try
      {
        if (paramString3.equals("r_csp"))
        {
          this.props.put("r_csp", URLDecoder.decode(this.text, "UTF-8"));
          this.text = "";
        }
        else if (paramString3.equals("r_time"))
        {
          this.props.put("r_time", URLDecoder.decode(this.text, "UTF-8"));
          this.text = "";
        }
        else if (paramString3.equals("r_ref"))
        {
          this.props.put("r_ref", URLDecoder.decode(this.text, "UTF-8"));
          this.text = "";
        }
        else if (paramString3.equals("r_error"))
        {
          this.props.put("r_error", URLDecoder.decode(this.text, "UTF-8"));
          this.text = "";
        }
        else if (paramString3.equals("r_ordernum"))
        {
          this.props.put("r_ordernum", URLDecoder.decode(this.text, "UTF-8"));
          this.text = "";
        }
        else if (paramString3.equals("r_message"))
        {
          this.props.put("r_message", URLDecoder.decode(this.text, "UTF-8"));
          this.text = "";
        }
        else if (paramString3.equals("r_code"))
        {
          this.props.put("r_code", URLDecoder.decode(this.text, "UTF-8"));
          this.text = "";
        }
        else if (paramString3.equals("r_tdate"))
        {
          this.props.put("r_tdate", URLDecoder.decode(this.text, "UTF-8"));
          this.text = "";
        }
        else if (paramString3.equals("r_score"))
        {
          this.props.put("r_score", URLDecoder.decode(this.text, "UTF-8"));
          this.text = "";
        }
        else if (paramString3.equals("r_authresponse"))
        {
          this.props.put("r_authresponse", URLDecoder.decode(this.text, "UTF-8"));
          this.text = "";
        }
        else if (paramString3.equals("r_approved"))
        {
          this.props.put("r_approved", URLDecoder.decode(this.text, "UTF-8"));
          this.text = "";
        }
        else if (paramString3.equals("r_avs"))
        {
          this.props.put("r_avs", URLDecoder.decode(this.text, "UTF-8"));
          this.text = "";
        }
      }
      catch (UnsupportedEncodingException localUnsupportedEncodingException)
      {
        this.result = localUnsupportedEncodingException.getMessage();
      }
    }

    public void startDocument()
      throws SAXException
    {
      this.text = new String();
    }

    public void endDocument()
      throws SAXException
    {
    }

    public void characters(char[] paramArrayOfChar, int paramInt1, int paramInt2)
      throws SAXException
    {
      if (this.text != null)
        this.text = new String(paramArrayOfChar, paramInt1, paramInt2);
    }

    public String getResult()
    {
      return this.result;
    }
  }

  private static class NullHostNameVerifier
    implements HostnameVerifier
  {
    public boolean verify(String paramString, SSLSession paramSSLSession)
    {
      return true;
    }
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.pos.payment.PaymentGatewayLinkPoint
 * JD-Core Version:    0.6.0
 */